package com.hxzy.tms.interceptor;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.hxzy.tms.HibernateSessionHolder;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@Deprecated
public class HibernateInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = -8598742796549307651L;
	private SessionFactory sessionFactory;

	@Override
	public void init() {
		super.init();
		Configuration configuration = new Configuration().configure();
		this.sessionFactory = configuration.buildSessionFactory();
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Session session = this.sessionFactory.openSession();

		String actionResult = null;
		try {
			HibernateSessionHolder.set(session);
			actionResult = invocation.invoke();
		} finally {
			HibernateSessionHolder.get().close();
		}

		return actionResult;
	}

	@Override
	public void destroy() {
		super.destroy();

		if (this.sessionFactory != null)
			this.sessionFactory.close();
	}

}
